/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PreferencesEdit.java
 *
 * Created on 15-oct-2011, 19:07:01
 */
package pfc.gui;

import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import org.apache.commons.configuration.BaseConfiguration;
import org.apache.commons.configuration.CompositeConfiguration;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import pfc.comun.IDEpfc;

/**
 *
 * @author xandru
 */
public class PreferencesEdit extends Editor {
    CompositeConfiguration conf;
    private JDialog dialog;
    /** Creates new form PreferencesEdit */
    public PreferencesEdit(CompositeConfiguration c) {
        initComponents();

        conf = c;
        loadPreferences();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        workdir = new javax.swing.JTextField();
        antHome = new javax.swing.JTextField();
        tomcatHome = new javax.swing.JTextField();
        javaHome = new javax.swing.JTextField();

        setMinimumSize(new java.awt.Dimension(342, 265));

        jLabel1.setText("Directorio de trabajo:");

        jLabel2.setText("Directorio de ANT:");

        jLabel3.setText("Directorio de TOMCAT:");

        jLabel4.setText("Directorio de JAVA:");

        jButton1.setText("Guardar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cerrar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Buscar ...");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Buscar ...");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setText("Buscar ...");
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jButton6.setText("Buscar ...");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        workdir.setEditable(false);
        workdir.setText("jTextField1");

        antHome.setEditable(false);
        antHome.setText("jTextField1");

        tomcatHome.setEditable(false);
        tomcatHome.setText("jTextField1");

        javaHome.setEditable(false);
        javaHome.setText("jTextField1");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(66, 66, 66)
                        .addComponent(jButton1)
                        .addGap(115, 115, 115)
                        .addComponent(jButton2))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel4))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(javaHome, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                                    .addComponent(antHome, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                                    .addComponent(workdir, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)
                                    .addComponent(tomcatHome, javax.swing.GroupLayout.DEFAULT_SIZE, 205, Short.MAX_VALUE)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(216, 216, 216)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton4)
                            .addComponent(jButton3)
                            .addComponent(jButton5)
                            .addComponent(jButton6))
                        .addGap(234, 234, 234))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(workdir, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(antHome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton4))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(tomcatHome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton5))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(javaHome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton6))
                .addGap(58, 58, 58)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton1))
                .addContainerGap(35, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        //WORKDIR
        JFileChooser fc = new JFileChooser();
        fc.setDialogTitle("Directorio de instalacion de TOMCAT");
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

        int seleccion = fc.showOpenDialog(IDEpfc.app);
        if (seleccion == fc.APPROVE_OPTION) {
            File workdirf = fc.getSelectedFile();
            IDEpfc.getConfiguration().setProperty("workdir", workdirf.getAbsolutePath());
            workdir.setText(workdirf.getAbsolutePath());
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        JFileChooser fc = new JFileChooser();
        fc.setDialogTitle("Directorio de instalacion de ANT");
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

        int seleccion = fc.showOpenDialog(IDEpfc.app);
        if (seleccion == fc.APPROVE_OPTION)
        {
            File antHomef = fc.getSelectedFile();
            File antJar = new File(antHomef + File.separator + "lib" + File.separator + "ant.jar");
            if (!antJar.exists())
                JOptionPane.showMessageDialog(IDEpfc.app, "El directorio proporcionado no contiene una instalacion valida de ANT");
            else {
                IDEpfc.getConfiguration().setProperty("anthome", antHomef);
                antHome.setText(antHomef.getAbsolutePath());

            }
        }
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
        JFileChooser fc = new JFileChooser();
        fc.setDialogTitle("Directorio de instalacion de TOMCAT");
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

        int seleccion = fc.showOpenDialog(IDEpfc.app);
        if (seleccion == fc.APPROVE_OPTION)
        {
            File tomcatHomef = fc.getSelectedFile();
            File tomcatJar = new File(tomcatHomef + File.separator + "lib" + File.separator + "catalina.jar");
            if (!tomcatJar.exists())
                JOptionPane.showMessageDialog(IDEpfc.app, "El directorio proporcionado no contiene una instalacion valida de TOMCAT");
            else {
                IDEpfc.getConfiguration().setProperty("tomcathome", tomcatHomef);
                tomcatHome.setText(tomcatHomef.getAbsolutePath());

            }
        }
    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        JFileChooser fc = new JFileChooser();
        fc.setDialogTitle("Directorio de instalacion de JAVA");
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

        int seleccion = fc.showOpenDialog(IDEpfc.app);
        if (seleccion == fc.APPROVE_OPTION)
        {
            File javaHomef = fc.getSelectedFile();
            File javaJar = new File(javaHomef + File.separator + "lib" + File.separator + "tools.jar");
            if (!javaJar.exists())
                JOptionPane.showMessageDialog(IDEpfc.app, "El directorio proporcionado no contiene una instalacion valida de JAVA");
            else {
                IDEpfc.getConfiguration().setProperty("javahome", javaHomef);
                javaHome.setText(javaHomef.getAbsolutePath());
            }
        }
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        dialog.setVisible(false);
        dialog.dispose();

    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        save();

        
    }//GEN-LAST:event_jButton1ActionPerformed

    public void save() {
        for (int i=0;i<conf.getNumberOfConfigurations();i++)
        {

            Configuration c = conf.getConfiguration(i);
            System.out.println(c.getClass());
            if (c.getClass()==PropertiesConfiguration.class)
            {
                PropertiesConfiguration c1 = (PropertiesConfiguration) c;
                try {
                    c1.save();
                } catch (ConfigurationException ex) {
                    Logger.getLogger(PreferencesEdit.class.getName()).log(Level.SEVERE, null, ex);
                }
            }            
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField antHome;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField javaHome;
    private javax.swing.JTextField tomcatHome;
    private javax.swing.JTextField workdir;
    // End of variables declaration//GEN-END:variables

    private void loadPreferences() {
        workdir.setText(conf.getString("workdir"));
        antHome.setText(conf.getString("anthome"));
        tomcatHome.setText(conf.getString("tomcathome"));
        javaHome.setText(conf.getString("javahome"));

    }

    void setDialog(JDialog dia) {
        dialog = dia;
    }
}
